//
// Created by 周智超 on 2020/6/9.
//

#include <iostream>
#include "gtest/gtest.h"
#include "stack_queue.h"

TEST(SqStack, curd)
{
    SqStack s;
    int x;
    initStack(s);
    ASSERT_EQ(1, isEmpty(s));
    ASSERT_EQ(0, pop(s, x));
    x = 10;
    ASSERT_EQ(1, push(s, x));
    ASSERT_EQ(10, s.data[s.top]);
    ASSERT_EQ(0, s.top);
    push(s, 20);
    ASSERT_EQ(1, pop(s, x));
    ASSERT_EQ(20, x);
}

TEST(LNode, curd)
{
    LNode *s;
    int x;
    initLStack(s);
    ASSERT_EQ(1, isLEmpty(s));
    ASSERT_EQ(0, lPop(s, x));
    x = 10;
    ASSERT_EQ(1, lPush(s, x));
    lPush(s, 20);
    ASSERT_EQ(1, lPop(s, x));
    ASSERT_EQ(20, x);
    ASSERT_EQ(1, lPop(s, x));
    ASSERT_EQ(10, x);
    ASSERT_EQ(0, lPop(s, x));
}


TEST(Calc, inCalc)
{
    char s[] = "3+4*5*(2+3)";
    int result;
    int success;
    success = calcInfix(s, result);
    ASSERT_EQ(success, 1);
    ASSERT_EQ(result, 103);
}

TEST(CalcFormat, in2pre)
{
    char s[] = "3+4*5*(2+3)";
    char s2[100] = "";
    int top2 = -1;
    int result, flag;
    infix2Prefix(s,  sizeof(s) / sizeof(s[0]) - 1, s2, top2);
    s2[top2+1] = '\0';
    std::cout << s2 << std::endl;
    ASSERT_STREQ(s2, "+3**45+23");
    flag = calcPrefix(s2, top2 + 1, result);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(result, 103);

    char ss1[] = "3+4*6/(2+1)";

    top2 = -1;
    infix2Prefix(ss1,  sizeof(ss1) / sizeof(ss1[0]) - 1, s2, top2);
    s2[top2+1] = '\0';
    std::cout << s2 << std::endl;
    ASSERT_STREQ(s2, "+3/*46+21");
    flag = calcPrefix(s2, top2 + 1, result);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(result, 11);
}

TEST(CalcFormat, in2suffix)
{
    char s[] = "3+4*5/(2+3)+8/(9*(3-1))";
    char s2[100] = "";
    int result, flag;

    int top2 = -1;
    in2suffix(s, s2, top2);
    ASSERT_STREQ(s2, "345*23+/+8931-*/+");

    char s1[] = "2+3*5-6";
    top2 = -1;
    in2suffix(s1, s2, top2);
    ASSERT_STREQ(s2, "235*+6-");
    flag = calcSuffix(s2, top2 + 1, result);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(result, 11);

    char ss1[] = "5*(2+3)";
    top2 = -1;
    in2suffix(ss1, s2, top2);
    ASSERT_STREQ(s2, "523+*");
    flag = calcSuffix(s2, top2 + 1, result);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(result, 25);

    char ss2[] = "3+4*5*(2+3)";
    top2 = -1;
    in2suffix(ss2, s2, top2);
    ASSERT_STREQ(s2, "345*23+*+");
    flag = calcSuffix(s2, top2 + 1, result);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(result, 103);

}

typedef  int ioLegalFunc(char io[]);

TEST(exercise, io)
{
    ioLegalFunc *ilf;
    ilf = ioLegal;
    int legal;
    char s1[] = "IO";
    legal = ilf(s1);
    ASSERT_EQ(legal, 1);
    char s2[] = "IOO";
    legal = ilf(s2);
    ASSERT_EQ(legal, 0);
    char s3[] = "IIO";
    legal = ilf(s3);
    ASSERT_EQ(legal, 0);

    char s4[] = "IIIOIOOIOO";
    legal = ioLegal(s4);
    ASSERT_EQ(legal, 1);

    char s5[] = "OI";
    legal = ioLegal(s5);
    ASSERT_EQ(legal, 0);
}


TEST(exercise, tailQueue)
{
    LNode *tq;
    int e;
    int flag;
    initTailQueue(tq);
    flag = deTailQueue(tq, e);
    ASSERT_EQ(flag, 0);
    enTailQueue(tq, 1);
    enTailQueue(tq, 3);
    enTailQueue(tq, 2);
    flag = deTailQueue(tq, e);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(e, 1);

    flag = deTailQueue(tq, e);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(e,3);

    flag = deTailQueue(tq, e);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(e, 2);

    flag = deTailQueue(tq, e);
    ASSERT_EQ(flag, 0);
}

TEST(exercise, tagQueue)
{
    TagQueue *q;
    initTagQueue(q);
    int flag, e;
    flag =  deTagQueue(q, e);
    ASSERT_EQ(flag, 0);
    flag = enTagQueue(q, 3);
    ASSERT_EQ(flag, 1);
    flag = enTagQueue(q, 1);
    ASSERT_EQ(flag, 1);
    flag = enTagQueue(q, 2);
    ASSERT_EQ(flag, 1);

    flag =  deTagQueue(q, e);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(e, 3);

    flag =  deTagQueue(q, e);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(e, 1);

    flag =  deTagQueue(q, e);
    ASSERT_EQ(flag, 1);
    ASSERT_EQ(e, 2);

    for(int i = 1; i <= maxSize; i++)
    {
        flag = enTagQueue(q, i);
        ASSERT_EQ(flag, 1);
    }

    flag = enTagQueue(q, maxSize + 1);
    ASSERT_EQ(flag, 0);

    for(int i = 1; i <= maxSize; i++)
    {
        flag = deTagQueue(q, e);
        ASSERT_EQ(flag, 1);
        ASSERT_EQ(e, i);
    }
}

TEST(exercise, dec2bin)
{
   int stack[maxSize]; int top=-1;
    int result = 0;
    int i = 1;
    int a = 11;
    int remainder;
    int quotient;
    do {
        quotient = a / 2;
        remainder = a % 2;
        a = quotient;
        stack[++top] = remainder;
    }while (a != 0);
   while (top != -1)
   {
       result += stack[top--] * i ;
       i = i * 10;
   }
    std::cout << result << std::endl;

}


TEST(exercise, bracket)
{
    int flag;
    flag = bracketMatch("[](){}");
    ASSERT_EQ(flag, 1);

    flag = bracketMatch("[]()'{]'{}");
    ASSERT_EQ(flag, 1);

    flag = bracketMatch("['qqqq[][}']()'{]'{}");
    ASSERT_EQ(flag, 1);
    flag = bracketMatch("'qqqq[][}']()'{]'{}");
    ASSERT_EQ(flag, 0);
}


int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);//将命令行参数传递给gtest
    return RUN_ALL_TESTS();   //RUN_ALL_TESTS()运行所有测试案例
}